
import axios from 'axios'

// 注意这里url的名称设置，const是不能修改的，如果定义为url的话，会与下面的有冲突
const rootUrl = '/webMenu'

export function delMenu (id, token) {
  const url = `${rootUrl}/${id}`
  // const url = `${rootUrl}/244`

  return axios({
    method: 'DELETE',
    url,
    headers: {
      Authorization: `Bearer ${token}\n`
    }
    // params
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

export function addMenu (name, pid, orderNum, status, token) {
  return axios({
    method: 'POST',
    url: rootUrl,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Authorization': `Bearer ${token}\n`
    },
    data: {
      name,
      pid,
      orderNum,
      status
    }
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// export function updateMenu (name, pid, id, level, status, token) {
export function updateMenu (data, token) {
  const url = `${rootUrl}/${data.id}`

  return axios({
    method: 'PUT',
    url,
    headers: {
      'Authorization': `Bearer ${token}\n`
    },
    data
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}
